package com.bosssoft.hr.javase;

import java.util.*;

/**
 * @Description: MyQueue 使用动态数组实现
 * @Author: WengYu
 * @CreateTime 2022/05/12 16:01
 */
public class MyQueue<T> implements Queue<T> {
    private List<T> queue = null;

    public MyQueue(){
        queue = new ArrayList<>();
    }

    @Override
    public boolean add(T data) {
        if (data == null){
            throw new RuntimeException("添加失败，元素不能为空");
        }
        return queue.add(data);
    }

    @Override
    public boolean offer(T data) {
        if (data == null){
            return false;
        }
        return queue.add(data);
    }

    @Override
    public T remove() {
        if (queue.isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return queue.remove(0);
    }

    @Override
    public T poll() {
        return queue.remove(0);
    }

    @Override
    public T element() {
        return queue.get(0);
    }

    @Override
    public T peek() {
        if (queue.isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return queue.get(0);
    }
}
